"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具清理dss内部的锁资源
Create At   : 2023/4/20
Owner       : opentestcase019
Description :
    1.从集群中踢出非当前节点，不指定-D DSS_HOME参数
    2.从集群中踢出非当前节点，指定-D DSS_HOME参数为正常值
    3.从集群中踢出非当前节点，指定错误的-D DSS_HOME参数
    4.从集群中踢出非当前节点，指定权限不足的-D DSS_HOME参数
    5.从集群中踢出当前节点
    6.在dss关闭的情况下执行从集群中踢出非当前节点
    7.在dss关闭的情况下执行从集群中踢出当前节点
    8.在集群停止的情况下执行
    9.DSS_HOME路径权限不足时，从集群中踢出非当前节点，不指定-D DSS_HOME参数
    10.从集群中踢出非当前节点，指定错误的dss实例id
    11.从集群中踢出非当前节点，指定在0-63范围内，但是不在集群中的dss实例id
Expect      :
    1.从集群中踢出非当前节点成功
    2.从集群中踢出非当前节点成功
    3.合理报错
    4.合理报错
    5.合理报错
    6.从集群中踢出非当前节点成功
    7.合理报错
    8.从集群中踢出非当前节点成功
    9.合理报错
    10.合理报错
    11.移除成功
History     :
"""
import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:从集群中踢出非当前节点，不指定-D DSS_HOME参数;" \
                "expect:从集群中踢出非当前节点成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};cm_ctl query -Cv;" \
                      f"dsscmd kickh -i 2"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 2',
                      execute_msg, "执行失败" + step1)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step1)
        self.logger.info("-----在备机执行-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd kickh -i 0"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 0.',
                      execute_msg, "执行失败" + step1)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step1)

        step2 = "-----step2:从集群中踢出非当前节点，指定-D DSS_HOME参数为正常值;" \
                "expect:从集群中踢出非当前节点成功-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 1.',
                      execute_msg, "执行失败" + step2)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step2)
        self.logger.info("-----在备机执行-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 0 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 0.',
                      execute_msg, "执行失败" + step2)
        for i in range(30):
            status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
            self.logger.info(status)
            if "OffLine" in status:
                break
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step2)

        step3 = "-----step3:从集群中踢出非当前节点，指定错误的-D DSS_HOME参数;" \
                "expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE}.cfg;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to kick host, kickid 1', execute_msg,
                      "执行失败" + step3)

        step4 = "-----step4:从集群中踢出非当前节点，" \
                "指定权限不足的-D DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"chmod 000 {macro.DSS_INSTANCE};" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE};" \
                      f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        res = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Failed to get vg entry info when alloc vg info",
                      res, "执行失败" + step4)
        self.assertIn("dss_inst.ini does not exist", res, "执行失败" + step4)

        step5 = "-----step5:从集群中踢出当前节点;expect:合理报错-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 0 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Failed to kick host, curr hostid 0, kick hostid 0.',
                      execute_msg, "执行失败" + step5)
        self.logger.info("-----在备机执行-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Failed to kick host, curr hostid 1, kick hostid 1.',
                      execute_msg, "执行失败" + step5)

        step6 = "-----step6:在dss关闭的情况下执行从集群中踢出非当前节点;" \
                "expect:从集群中踢出非当前节点成功-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd stopdss;sleep 2;" \
                      f"dsscmd kickh -i 1;" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg, "执行失败" + step6)
        self.assertEquals(2, execute_msg.count(
            "Succeed to kick host, kickid 1"), "执行失败" + step6)
        time.sleep(60)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step6)

        step7 = "-----step7:在dss关闭的情况下执行从集群中踢出当前节点;expect:合理报错-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd stopdss;sleep 2;" \
                      f"dsscmd kickh -i 0;" \
                      f"dsscmd kickh -i 0 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to stop server.', execute_msg,
                      "执行失败" + step7)
        self.assertEquals(2, execute_msg.count(
            "Failed to kick host, curr hostid 0, kick hostid 0."),
                          "执行失败" + step7)

        step8 = "-----step8:在集群停止的情况下执行;expect:从集群中踢出非当前节点成功-----"
        self.logger.info(step8)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 1;" \
                      f"dsscmd kickh -i 1 -D {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 1.',
                      execute_msg, "执行失败" + step8)
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败')

        step9 = "-----step9:DSS_HOME路径权限不足时，从集群中踢出非当前节点，" \
                "不指定-D DSS_HOME参数;expect:合理报错-----"
        self.logger.info(step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"chmod 000 {macro.DSS_INSTANCE};" \
                      f"dsscmd kickh -i 1;" \
                      f"chmod 700 {macro.DSS_INSTANCE}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Failed to get vg entry info when alloc vg info",
                      execute_msg, "执行失败" + step9)
        self.assertIn("dss_inst.ini does not exist", execute_msg,
                      "执行失败" + step9)

        step10 = "-----step10:从集群中踢出非当前节点，指定错误的dss实例id;expect:合理报错-----"
        self.logger.info(step10)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 64;" \
                      f"dsscmd kickh -i 64 -D {macro.DSS_INSTANCE};" \
                      f"dsscmd kickh -i a;" \
                      f"dsscmd kickh -i @1"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('The value of inst_id should be in [0, 64).',
                      execute_msg, "执行失败" + step10)
        self.assertEquals(2, execute_msg.count(
            'The value of inst_id is invalid.'), "执行失败" + step10)
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step10)

        step11 = "-----step11:从集群中踢出非当前节点，指定在0-63范围内，" \
                 "但是不在集群中的dss实例id;expect:移除成功-----"
        self.logger.info(step11)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd kickh -i 63;" \
                      f"dsscmd kickh -i 63 -D {macro.DSS_INSTANCE};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.standby_node, execute_cmd)
        self.assertIn('Succeed to kick host, kickid 63.',
                      execute_msg, "执行失败" + step7)
        time.sleep(30)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn("cluster_state   : Normal", status, "执行失败" + step11)

    def tearDown(self):
        self.logger.info("-----无需清理环境-----")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
